﻿using CPPEI.Coolzon.UI.Script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI
{
    public partial class TabContainerPart
    {
        public const string VS_TAB = "tab";

        /// <summary>
        /// 是否具有视图状态，如果有那么就以视图状态里的信息为准
        /// </summary>
        private bool _hasViewState = false;

        public override void LoadViewState(Coolzon.ViewState.ViewState viewState)
        {
            Coolzon.ViewState.PartState partState = viewState.GetPartState(this.XFullName);
            if (partState != null)
            {
                _hasViewState = true;

                #region load tab state
                string tabString = partState.GetString(VS_TAB);
                if (!string.IsNullOrEmpty(tabString))
                {
                    this.SelectTab(Convert.ToInt32(tabString));
                }
                #endregion
            }

            #region load sub part viewstate
            for (int index = 0; index < this._partCollection.Count; index++)
            {
                UI.Part subPart = this._partCollection[index];

                subPart.LoadViewState(viewState);
            }
            #endregion

            base.LoadViewState(viewState);
        }

        public override void SaveViewState(Coolzon.ViewState.ViewState viewState)
        {
            Coolzon.ViewState.PartState partState = viewState.GetPartState(this.XFullName);
            if (partState == null)
            {
                partState = new Coolzon.ViewState.PartState(this.XFullName);
                viewState.AddPartState(partState);
            }

            #region save tab state
            partState.SetInt(VS_TAB, this.ActiveTabIndex);
            #endregion

            #region save sub part viewstate
            for (int index = 0; index < this._partCollection.Count; index++)
            {
                UI.Part subPart = this._partCollection[index];
                subPart.SaveViewState(viewState);
            }
            #endregion

            base.SaveViewState(viewState);
        }
    }
}
